/*
 * @(#)ProfileInfoTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

package com.dcivision.dms.taglib;

import java.sql.Connection;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.dcivision.dms.dao.DmsDocumentDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;

/**
 * <p>Class Name:    ProfileInfoTag.java    </p>
 * <p>Description:   This tag is the layout the dms detail according to the documentID in DOCUMENT_ID
 * @author           Phoebe Wong
 * @company          DCIVision Limited
 * @creation date    08/08/2003
 * @version          $Revision: 1.9 $
 */

public class ProfileInfoTag extends BodyTagSupport {

  public static final String REVISION = "$Revision: 1.9 $";

  private String sDocumentID;

  /**
   * Constructor
   */
  public ProfileInfoTag() {
    super();
  }

  public String getDocumentID() {
    return this.sDocumentID;
  }


  public void setDocumentID(String sDocumentID) {
    this.sDocumentID = sDocumentID;
  }

  /**
   * Method called at start of tag
   * @return a SKIP_BODY
   */
  public int doStartTag() throws JspException {
    return SKIP_BODY;
  }

  public int doEndTag() throws JspException{
    JspWriter out = pageContext.getOut();
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    Connection conn = (Connection)pageContext.getRequest().getAttribute(GlobalConstant.DB_KEY);
    DmsDocumentDAObject dmsDocmentDAO = new DmsDocumentDAObject(sessionContainer, conn);
    try {
      List result = dmsDocmentDAO.getDocumentProfileInfo(new Integer(this.sDocumentID));
      if (!(((String[])result.get(0))[0]==null)){
        out.println("<table>");
        out.println("  <tr>");
        out.println("    <td>" + MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "dms.label.name_label_" + ((String[])result.get(0))[2]) + ":</td>"); // dms.label.name_label_<doc_type>
        out.println("    <td>" + ((String[])result.get(0))[0] + "</td>");
        out.println("  </tr>");
        out.println("  <tr>");
        out.println("    <td>" + MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "dms.label.number_of_versions") + ":</td>"); // dms.label.number_of_versions
        out.println("    <td>" + ((String[])result.get(0))[1] + "</td>");
        out.println("  </tr>");
        for (int i = 1; i<result.size(); i++) {
          String[] saInfo = (String[])result.get(i);
          String sCurrent = ((String[])result.get(i))[0];
          String sPrevious = ((String[])result.get(i-1))[0];
          if (!(sCurrent.equals(sPrevious))){
            out.println("  <tr>");
            out.println("    <td>" + MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "dms.label.profile_name") + ":</td>"); // dms.label.profile_name
            out.println("    <td>"+ saInfo[0] + "</td>");
            out.println("  </tr>");
          }
          if (saInfo[0]!=null){
            out.println("  <tr>");
            out.println("    <td>"+ saInfo[1] + ":</td>");
            out.println("    <td>"+ saInfo[2] + "</td>");
            out.println("  </tr>");
          }
        }
        out.println("</table>");
      }
    } catch (ApplicationException appEx) {
      throw new JspException(appEx);
    } catch (java.io.IOException ioe) {
      throw new JspException(ioe);
    }
    return EVAL_PAGE;
  }

  public void release() {
    super.release();
    sDocumentID=null;
  }
}
